package com.hdc.hdc.warehouseManage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.hdc.hdc.warehouseManage.entity.GoodsTrade;
import com.hdc.hdc.warehouseManage.entity.House;
import com.hdc.hdc.warehouseManage.entity.Position;
import com.hdc.hdc.warehouseManage.entity.vo.WareHouseVO;
import com.hdc.hdc.warehouseManage.repository.HouseRepository;
import com.hdc.hdc.warehouseManage.repository.PositionRepository;
import com.hdc.hdc.warehouseManage.repository.WareHouseRepository;
import com.hdc.hdc.warehouseManage.service.WareHouseServiceI;
import com.hdc.hdc.warehouseManage.utill.IsNotNullValit;

/**
 * 仓库管理服务类
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月16日 下午5:15:33 
 *
 */
@Service
public class WareHouseServiceImpl implements WareHouseServiceI {
	
	@Autowired
	private WareHouseRepository wareHouseRepository;
	
	@Autowired
	private HouseRepository houseRepository;
	
	@Autowired
	private PositionRepository positionRepository;
	
	@Resource
    private EntityManagerFactory sessionFactory;
	
	@Override
	public Page<GoodsTrade> findWareHouseForPage(Integer pageNum, Integer pageSize) {
		
		Pageable pb = PageRequest.of(pageNum-1, pageSize);
		
		Page<GoodsTrade> goods = this.wareHouseRepository.findAll(pb);
		
		return goods;
	}

	@Override
	public Page<House> findHouseForPage(Integer pageNum, Integer pageSize) {

		Pageable pb = PageRequest.of(pageNum-1, pageSize);
		
		Page<House> houses = this.houseRepository.findAll(pb);
		
		return houses;
	}

	@Override
	public Page<Position> findPositionForPage(Integer pageNum, Integer pageSize) {

		Pageable pb = PageRequest.of(pageNum-1, pageSize);
		
		Page<Position> positions = this.positionRepository.findAll(pb);
		
		return positions;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsTrade> queryByVo(WareHouseVO vo) {
		
		List<GoodsTrade> goods = new ArrayList<GoodsTrade>(50);
		
		EntityManager em = this.sessionFactory.createEntityManager();
		
		Map<String, String> params = new HashMap<String, String>();
		
		StringBuffer hql = new StringBuffer("from GoodsTrade t where 1=1 ");
		if (IsNotNullValit.StringIsNotNull(vo.getHouseName())) {
			hql.append(" and t.houseName like :houseName");
			params.put("houseName", "%"+vo.getHouseName()+"%");
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getHouseId())) {
			hql.append(" and t.houseId = :houseId");
			params.put("houseId", vo.getHouseId());
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getPositionName())) {
			hql.append(" and t.positionName like :positionName");
			params.put("positionName", "%"+vo.getPositionName()+"%");
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getPositionId())) {
			hql.append(" and t.positionId = :positionId");
			params.put("positionId", vo.getPositionId());
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getLocationName())) {
			hql.append(" and t.goodsLocationName like :goodsLocationName");
			params.put("goodsLocationName", "%"+vo.getLocationName()+"%");
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getLocationId())) {
			hql.append(" and t.locationId = :locationId");
			params.put("locationId", vo.getLocationId());
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getGoodsName())) {
			hql.append(" and t.goodsName like :goodsName");
			params.put("goodsName", "%"+vo.getGoodsName()+"%");
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getSku())) {
			hql.append(" and t.sku like :sku");
			params.put("sku", "%"+vo.getSku()+"%");
		}
		
//		hql += " limit :pageNum, :pageSize";
		
		Query query = em.createQuery(hql.toString());
		
		Iterable<String> it = params.keySet();
		for (String key : it) {
			if (key != null) {
				query.setParameter(key, params.get(key));
			}
		}
		
		query.setFirstResult((vo.getPageNum()-1)*vo.getPageSize());
		query.setMaxResults(vo.getPageSize());
		
		
		goods = query.getResultList();
		
		return goods;
	}

}
